package com.sinocare.project.maintenance.util;

import com.sinocare.project.common.entity.SccMenu;
import com.sinocare.project.common.entity.SccRoleMenu;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

public class JsonList {
    public static Map<String, Object> getJson(List<SccMenu> list, List<SccRoleMenu> roleMenuList) {
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).getUrl().equals("null") || list.get(i).getUrl().contains("#")) {
                list.get(i).setUrl("");
            }
        }
        Map<String, Object> map = new LinkedHashMap<>();
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).getMenuName().equals("常规管理")) {
                Map<Object, Object> a = new LinkedHashMap<>();
                a.put("title", list.get(i).getMenuName());
                a.put("icon", list.get(i).getIcon());
                a.put("href", list.get(i).getUrl());
                a.put("target", list.get(i).getTarget());
                List<Map<Object, Object>> k = new ArrayList<>();
                for (int i1 = 0; i1 < list.size(); i1++) {
                    if (list.get(i1).getPId() == 0 && list.get(i1).getType().equals("item")) {
                        Map<Object, Object> b = new LinkedHashMap<>();
                        for (int j = 0; j < roleMenuList.size(); j++) {
                            if(list.get(i1).getMenuId()==roleMenuList.get(j).getMenuId()){
                                b.put("title", list.get(i1).getMenuName());
                                b.put("href", list.get(i1).getUrl());
                                b.put("icon", list.get(i1).getIcon());
                                b.put("target", list.get(i1).getTarget());
                                k.add(b);
                                break;
                            }
                        }
                        List<Map<Object, Object>> m = new ArrayList<>();
                        for (int i2 = 0; i2 < list.size(); i2++) {
                            if (list.get(i2).getPId() == list.get(i1).getMenuId()) {
                                Map<Object, Object> c = new LinkedHashMap<>();
                                for (int j = 0; j < roleMenuList.size(); j++) {
                                    if(list.get(i2).getMenuId()==roleMenuList.get(j).getMenuId()){
                                        c.put("title", list.get(i2).getMenuName());
                                        c.put("href", list.get(i2).getUrl());
                                        c.put("icon", list.get(i2).getIcon());
                                        c.put("target", list.get(i2).getTarget());
                                        m.add(c);
                                        break;
                                    }
                                }

                            }
                        }
                        b.put("child", m);
                    }
                }
                a.put("child", k);
                List<Map<Object, Object>> pp = new ArrayList<>();
                pp.add(a);
                map.put("menuInfo", pp);
            } else if (list.get(i).getMenuName().equals("Sinocare")) {
                Map<Object, Object> a = new LinkedHashMap<>();
                a.put("title", list.get(i).getMenuName());
                a.put("image", list.get(i).getUrl());
                map.put("logoInfo", a);
            } else if (list.get(i).getMenuName().equals("首页")) {
                Map<Object, Object> a = new LinkedHashMap<>();
                a.put("title", list.get(i).getMenuName());
                a.put("href", list.get(i).getUrl());
                map.put("homeInfo", a);
            }
        }
        return map;
    }

    public static List<Map<Object,Object>> setMenu(List<SccMenu> list,List<SccRoleMenu> menu) {
        List<Map<Object,Object>>cnm=new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).getPId() == 0 && list.get(i).getType().equals("item")) {
                Map<Object, Object> b = new LinkedHashMap<>();
                b.put("title", list.get(i).getMenuName());
                b.put("id", list.get(i).getMenuId());
                b.put("field","");
                b.put("checked",false);
                b.put("spread",false);
                List<Map<Object, Object>> m = new ArrayList<>();
                for (int i1 = 0; i1 < list.size(); i1++) {
                    if (list.get(i1).getPId() == list.get(i).getMenuId()) {
                        Map<Object, Object> c = new LinkedHashMap<>();
                        c.put("title", list.get(i1).getMenuName());
                        c.put("id", list.get(i1).getMenuId());
                        c.put("field", "");
                        boolean fag=false;
                        for (int j = 0; j < menu.size(); j++) {
                            if(menu.get(j).getMenuId()==list.get(i1).getMenuId()){
                                fag=true;
                                System.out.println("fag:"+fag);
                                break;
                            }
                        }
                        c.put("checked",fag);
                        c.put("spread", true);
                        m.add(c);
                    }
                }
                b.put("children", m);
                cnm.add(b);
            }
        }
        return cnm;
    }
}
